clear all
cap close
set more off

cd ".\Data_Program"

use county,clear

* merge with division data
merge m:1 county using Econdata\countytodivision
keep if _merge==3 
drop _merge

* merge with neighbor data
merge 1:1 quarter county using sci_10nb.dta
keep if _merge==3
drop _merge

* merge with Mintel data (proprietary)
merge 1:1 county quarter using Proprietary\mintel
drop if _merge==2 
drop _merge 
replace finad=0 if finad==.
replace nonfinad=0 if nonfinad==.
replace ad=0 if ad==.
drop ad nonfinad

* merge with county's own economic data

    * CoreLogic house price growth (proprietary)
merge 1:1 quarter county using Proprietary\hpi_county_quarter_exp
drop if _merge==2
drop _merge
replace hpgr=hpgr4
drop qhpi hpgr1 hpgr4

    * Census Buearu population growth
gen year=yofd(dofq(quarter))	
merge m:1 year county using Econdata\pop_county
drop if _merge==2
drop _merge
drop fracm frach

    * QCEW employment growth
merge m:1 year county using Econdata\emp_county_year
drop if _merge==2
drop _merge 

    * CCP subprime share
merge 1:1 quarter county using Proprietary\subprime_county_quarter
drop if _merge==2  
drop _merge 

     * Census education level in 2010
merge m:1 county using Econdata\edu
drop if _merge==2  
drop _merge 

* demographic X year controls
tab year, gen(yy)
foreach i of numlist 1/10{
   gen baboveyy`i' = babove*yy`i'
   gen fracbyy`i' = fracb*yy`i'
   gen fracyyy`i' = fracy*yy`i'

foreach b of numlist 1/10{ 
   gen babove2yy`i'`b' = babove2`b'*yy`i'
}
}

* IRS migration flows
merge m:1 year county using Econdata\migration
drop if _merge==2  
drop _merge

* Regressions
xtset county quarter

* weights
sort county quarter
by  county: egen w1 = mean(tot)
by  county: egen w2 = mean(pop)
gen w=w1


  * outcome var--change in finshare
foreach x of varlist finshare finshare_refi finshare_pur finshare2*{
gen d`x'=`x'-l4.`x'
}

gen dfinad = (log(finad+1)-log(l4.finad+1))/100
foreach b of numlist 1/10{ 
   gen dfinad2`b' = (log(finad2`b'+1)-log(l4.finad2`b'+1))/100
}


*===============================================================================
* Regression
*===============================================================================

tempname sci_all
postfile `sci_all' b b_sci_all se_sci_all using sci_all.dta,replace

tempname sci_refi
postfile `sci_refi' b b_sci_refi se_sci_refi using sci_refi.dta,replace

tempname sci_pur
postfile `sci_pur' b b_sci_pur se_sci_pur using sci_pur.dta,replace

forval b= 1/10{
    
local varlist="dfinshare2 hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 dfinad2 babove2yy1 babove2yy2 babove2yy3 babove2yy4 babove2yy5 babove2yy6 babove2yy7 babove2yy8 babove2yy9 babove2yy10"
foreach x of local varlist{
gen `x'=`x'`b'
}

local ctrl "hpgr popgr empgr fracyyy* fracbyy* subprime baboveyy* dfinad hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 dfinad2 babove2yy1 babove2yy2 babove2yy3 babove2yy4 babove2yy5 babove2yy6 babove2yy7 babove2yy8 babove2yy9 linflow loutflow"

reghdfe dfinshare dfinshare2 `ctrl' [aw=w],a(county division#quarter) cluster(county)
post `sci_all' (`b') (_b[dfinshare2]) (_se[dfinshare2])

reghdfe dfinshare_refi dfinshare2 `ctrl' [aw=w],a(county division#quarter) cluster(county)
post `sci_refi' (`b') (_b[dfinshare2]) (_se[dfinshare2])

reghdfe dfinshare_pur dfinshare2 `ctrl' [aw=w],a(county division#quarter) cluster(county)
post `sci_pur' (`b') (_b[dfinshare2]) (_se[dfinshare2])


drop dfinshare2 hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 dfinad2 babove2yy1 babove2yy2 babove2yy3 babove2yy4 babove2yy5 babove2yy6 babove2yy7 babove2yy8 babove2yy9 babove2yy10
}
postclose `sci_all'
postclose `sci_refi'
postclose `sci_pur'


*===============================================================================
* plot Regression Results
*===============================================================================

use dis_all,clear
merge 1:1 b using dis_refi
drop _merge
merge 1:1 b using dis_pur
drop _merge
merge 1:1 b using sci_all
drop _merge
merge 1:1 b using sci_refi
drop _merge
merge 1:1 b using sci_pur
drop _merge

scalar d = 1.96
local varlist="dis_all dis_refi dis_pur sci_all sci_refi sci_pur"
foreach x of local varlist{
	gen lb_`x'=b_`x'-d*se_`x'
	gen ub_`x'=b_`x'+d*se_`x'
	erase `x'.dta
}


gen b1=b+0.2

* Figure 2(a)
twoway(scatter b_dis_all b, msymbol(S) mcolor(black) )(rcap lb_dis_all ub_dis_all b, lcolor(black)), graphregion(color(white)) xtitle("Top 10 GCMs",size(5)) ytitle("Network effect (pp)",size(5)) xlabel(1(1)10.2,labsize(5)) ylabel(0(0.03)0.12,labsize(5)) legend(off)  scheme(s2color)

* Figure 2(b)
twoway(scatter b_sci_all b, msymbol(S) mcolor(black) )(rcap lb_sci_all ub_sci_all b, lcolor(black)), graphregion(color(white)) xtitle("Top 10 SCMs",size(5)) ytitle("Network effect (pp)",size(5)) xlabel(1(1)10.2,labsize(5)) ylabel(0(0.015)0.06,labsize(5)) legend(off)  scheme(s2color)
